package com.study.mq.send;

import java.util.Scanner;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.study.utils.ConnectionUtils;

public class SendMsg
{
    private final static String EXCHANGE_NAME = "exchange-direct";
    
    public static void main(String[] args) throws Exception{
        System.out.println("请输入消息：");
        Scanner scanner = new Scanner(System.in);
        String msg = null;
        while(!"quit".equals(msg = scanner.nextLine())){
            Connection connection = ConnectionUtils.getConnection();
            Channel channel = connection.createChannel();
            
            // 声明exchange，指定类型为fanout
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, false, null);
            //System.out.println(BuiltinExchangeType.FANOUT);
            
            if(msg.startsWith("key1")){
                channel.basicPublish(EXCHANGE_NAME,"consumer-key1",null,msg.getBytes());
            }else if(msg.startsWith("key2")){
                channel.basicPublish(EXCHANGE_NAME,"consumer-key2",null,msg.getBytes());
            }
            System.out.println("发送：" + msg);
            
            channel.close();
            connection.close();
        }
    }
}
